package com.kistring.polygyny;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.widget.ArrayAdapter;

public class PolygynyActivity extends ListActivity implements SensorEventListener {
    
	private static final Uri URI_SMS = Uri.parse("content://sms/inbox");
	private List<String> items = null;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.polygyny);
		// create the database 
		
//		new Thread(giveToken).start();
		startService(new Intent(this, Kitsai.class));
	}

	@Override
	protected void onResume() {
		super.onResume();
		SmsManager sms = SmsManager.getDefault();
		Cursor c = getContentResolver().query(URI_SMS, null, null, null, null);
		// Object messages[] = (Object[]) savedInstanceState.get("pdus");

		// SmsMessage smsMessage[] = new SmsMessage[messages.length];
		// for (int n = 0; n< messages.length; n++) {
		// smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
		// }
		items = new ArrayList<String>();
		String[] nobody = c.getColumnNames();

		if (c.moveToFirst()) {
			// Toast.makeText(this,
			// c.getColumnNames()
			int bodyIdx = c.getColumnIndex("_id"); // "address", "person",
													// "body"
			do {
				String body = c.getString(bodyIdx);
				// long thread_id = c.getLong(1);
				items.add(String.valueOf(body));
			} while (c.moveToNext());
		}
		// Uri toBeDeleted = Uri.parse("content://sms/inbox/"
		// + c.getLong(0));
		// getContentResolver().delete(toBeDeleted, null, null);

		ArrayAdapter<String> fileList = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_multiple_choice, items);
		setListAdapter(fileList);
		// Toast toast = Toast.makeText(this, "", Toast.LENGTH_LONG);
		// toast.show();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		stopService(new Intent(this, Kitsai.class));
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSensorChanged(SensorEvent event) {
 
	}

}